﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using CXMLStudioIDE.Common.Entities;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace CXMLStudioIDE.Common
{
    public enum DisplayContext
    {
        Deafult = 1,
        FormDesigner = 2,
        GoldCXML = 3,
        MappingForm = 4
    };
    public enum ControlType
    { 
        Control = 1,
        CXMLTreeNode = 2
    }

    public class Common
    {
        #region Methods

        static string[] ParseText(string strText)
        {
            return strText.Length > 0 ? strText.Replace(" ", "").Split(new char[] { ',' }) : null;
        }
        
        static AttributeCollection ParseAttributes(string[] categorynames)
        {
            if (categorynames == null) return null;
            Attribute[] attributes = new Attribute[categorynames.Length];
            for (int iattribute = 0; iattribute < categorynames.Length; iattribute++)
            {
                attributes[iattribute] = new CategoryAttribute(categorynames[iattribute]);
            }
            return new AttributeCollection(attributes);
        }

        public static Type GetTypeFromString(string s_Type)
        {
            Type t = null;
            Assembly[] appAssemblies = System.AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in appAssemblies)
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.ToString().Equals(s_Type))
                    {
                        t = type;
                        break;
                    }
                }
                if (t != null)
                    break;
            }
            return t;
        }

        public static List<Type> GetTypes()
        {
            List<Type> types = new List<Type>();
            Assembly[] appAssemblies = System.AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in appAssemblies)
            {
                foreach (Type type in assembly.GetTypes())
                {
                     types.Add(type);
                }
            }
            return types;
        }

        public static string GetNetworkUserId()
        {
            string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
            if (user.IndexOf("\\") > 0)
            {
                user = user.Substring(user.IndexOf("\\") + 1);
            }
            return user;
        }

        public static string GetLineageFromControlName(string sZ_ControlName)
        {
            string lineage = null;
            if (sZ_ControlName.Contains("_") && sZ_ControlName.Length > 1)
            {
                lineage = sZ_ControlName.Substring(sZ_ControlName.IndexOf("_") + 1);
            }
            return lineage;
        }
        public static int GetEntityTypeVersionIDFromControlName(string sZ_ControlName)
        {
            int etvid = -1;
            if(sZ_ControlName.Contains("_"))
            {
                string[] array = sZ_ControlName.Split('_');
                if (array.Count() > 0)
                {
                    etvid = Convert.ToInt32(array.Last());
                }
            }
            return etvid;
        }
        public static bool IsSubclassOfRawGeneric(Type generic, Type toCheck)
        {
            while (toCheck != typeof(object))
            {
                var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
                if (generic == cur)
                {
                    return true;
                }
                toCheck = toCheck.BaseType;
            }
            return false;
        }

        public static EntityVersion ParseEntityVersion(string sZ_EntityVersion)
        {
            EntityVersion ev = null;
            if (sZ_EntityVersion.Contains('.'))
            {
                string[] array = sZ_EntityVersion.Split('.');
                if (array.Count() == 3)
                {
                    ev = new EntityVersion(-1,array[0], array[1], array[2]);
                }
            }
            return ev;
        }

        public static object FromXml(string Xml, System.Type ObjType)
        {
            XmlSerializer ser;
            ser = new XmlSerializer(ObjType);
            StringReader stringReader;
            stringReader = new StringReader(Xml);
            XmlTextReader xmlReader;
            xmlReader = new XmlTextReader(stringReader);
            object obj;
            obj = ser.Deserialize(xmlReader);
            xmlReader.Close();
            stringReader.Close();
            return obj;

        }

        public static string ToXML(object Obj, XmlSerializerNamespaces namespaces)
        {
            XmlSerializer ser;
            ser = new XmlSerializer(Obj.GetType());
            MemoryStream memStream;
            memStream = new MemoryStream();
            XmlTextWriter xmlWriter;
            xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            xmlWriter.Namespaces = true;
            ser.Serialize(xmlWriter, Obj, namespaces);
            xmlWriter.Close();
            memStream.Close();
            string xml;
            xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            return xml;
        }
        public static string GetLabelNameFromBindingName(string bindingname)
        { 
            string labelname = bindingname;
            if(bindingname.Contains('.'))
            {
                string[] array = bindingname.Split('.');
                labelname = array[array.Length - 1];
            }
            return labelname;
        }
        public static DialogResult InputBox(string title, string promptText, ref string value)
        {
            Form form = new Form();
            Label label = new Label();
            TextBox textBox = new TextBox();
            Button buttonOk = new Button();
            Button buttonCancel = new Button();

            form.Text = title;
            label.Text = promptText;
            textBox.Text = value;

            buttonOk.Text = "OK";
            buttonCancel.Text = "Cancel";
            buttonOk.DialogResult = DialogResult.OK;
            buttonCancel.DialogResult = DialogResult.Cancel;

            label.SetBounds(9, 20, 372, 13);
            textBox.SetBounds(12, 36, 372, 20);
            buttonOk.SetBounds(228, 72, 75, 23);
            buttonCancel.SetBounds(309, 72, 75, 23);

            label.AutoSize = true;
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
            buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 107);
            form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
            form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = buttonOk;
            form.CancelButton = buttonCancel;

            DialogResult dialogResult = form.ShowDialog();
            value = textBox.Text;
            return dialogResult;
        }
        #endregion
    }
}
